<?php
/*
 * @Author: your name
 * @Date: 2019-10-22 16:53:17
 * @LastEditTime : 2020-01-09 17:50:20
 * @LastEditors  : Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /ahsmp_3/application/common/service/HostLearnConfig.php
 */

namespace app\common\service;

class HostLearnConfig extends Base 
{
    /**
     * @name: 添加
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 11:09:15
     */
    function add ($host_id, $osversion)
    {
        $config = $this->ruleAndFirewall($host_id, $osversion);
        $return = $config['use_rule'];
        if(db('HostLearnConfig')->where('host_id', $host_id)->count() > 0)
        {
            return $return;
        }
        db('HostLearnConfig')->insert($config);

        return $return;
    }

    /**
     * @name: 规则和防火墙
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-06 10:28:05
     */
    private function ruleAndFirewall ($host_id, $osversion)
    {
        $return = [
            'host_id' => $host_id,
            'use_rule' => 1,//linux
            'firewall' => '111',//全开
        ];

        if(!empty($osversion))
        {
            if(version_compare($osversion, '6.2') > -1)
            {
                $return['use_rule'] = 4;//2012windows以上
            }
            else
            {
                if (version_compare($osversion, '6.0') > -1)
                {
                    $return['use_rule'] = 3;//windows2008
                }
                else
                {
                    $return = [
                        'host_id' => $host_id,
                        'use_rule' => 2,//windows2003
                        'firewall' => '000',//全关
                    ];
                }
            }
        }

        return $return;
    }
    /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-24 11:26:09
     */
    function del ($host_id_a)
    {
        db('HostLearnConfig')->whereIn('host_id', $host_id_a)->delete();
    }

    /**
     * @name: 修改学习配置
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 10:38:46
     */
    function modifyStudyConfig ($host_id, $data)
    {
        $time = time();
        $data['stime'] = $time;
        $data['etime'] = $time + $data['keep_time'];
        db('HostLearnConfig')->whereIn('host_id', $host_id)->update($data);
    }

    /**
     * @name: 查找单个
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 10:55:03
     */
    function findOne ($host_id)
    {
        $return = db('HostLearnConfig')->where('host_id', $host_id)->find();

        return $return;
    }

    /**
     * @name: 开启学习
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 11:14:19
     */
    function learnOn ($host_id)
    {
        $time = time();
        $data['stime'] = $time;
        $data['etime'] =  db()->raw('keep_time + ' . $time);
        $data['learn_status'] = 1;
        db('HostLearnConfig')->whereIn('host_id', $host_id)->update($data);
    }

    /**
     * @name: 关闭学习
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 11:14:19
     */
    function learnOff ($host_id)
    {
        
        $data['etime'] =  time();
        $data['learn_status'] = 0;
        db('HostLearnConfig')->whereIn('host_id', $host_id)->update($data);
    }

    /**
     * @name: netfilter 日志状态
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-24 13:38:34
     */
    function netfilterLog ($host_id, $status)
    {
        db('HostLearnConfig')->whereIn('host_id', $host_id)->update(['log_status' => $status]);
    }
    /**
     * @name: 初始化
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 14:34:37
     */
    function initialization ($host_id)
    {
        db('HostLearnConfig')->whereIn('host_id', $host_id)->update(['init' => 1]);
    }

    /**
     * @name: 防火墙修改
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 14:55:24
     */
    function firewallConfigModify ($host_id, $firewall)
    {
        db('HostLearnConfig')->whereIn('host_id', $host_id)->update(['firewall' => $firewall]);
    }

        /**
     * @name: 自动停止学习
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-28 11:25:37
     */
    function autoStopLearn ($host_id_a)
    {
        db('HostLearnConfig')->where('etime', '<=', time())->where('learn_status', 1)->update(['learn_status' => 0]);
        $host_id_a ? db('HostLearnConfig')->whereIn('host_id', $host_id_a)->update(['learn_status' => 0]) : '' ;
    }

    /**
     * @name: 修改日志状态
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-06 17:35:27
     */
    function modifyLogStatus($host_id, $status)
    {
        $res = db('HostLearnConfig')->where('host_id', $host_id)->update(['log_status' => $status]);
        return $res;
    }

    /**
     * @name: 查询列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-06 11:45:54
     */
    function selectByHostId($host_id_a, $field = '*')
    {
        $return = db('HostLearnConfig')->field($field) ->whereIn('host_id', $host_id_a)->select();

        return $return;
    }

    /**
     * @name: 清除提醒
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-08 16:30:45
     */
    function clearReming ($host_id)
    {
        db('HostLearnConfig')->whereIn('host_id', $host_id)->update(['virtual_rule_hit_remind' => 0]);
    }
}